package com.eshop.common.util.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Iterator;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;

public class SearchTest {
	private static String getHtmlData(String url) { // 传入的页面地址 url
		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					getStream(url), Charset.forName("UTF-8")));// 编码格式根据页面去定
			String s = null;

			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString(); // 返回页面的html 信息
	}
	
	private static java.io.InputStream getStream(String url) throws IOException{
		java.net.URL connUrl = new URL(url);
        java.net.HttpURLConnection conn = (HttpURLConnection) connUrl.openConnection();
        conn.setRequestProperty("User-agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)");
        java.io.InputStream input = conn.getInputStream();
        return input;
	}

	public static void main(String[] args) {
//		try {
//			Document doc = Jsoup.connect("http://www.feiniu.com/category/C28892/?sort=recommend&q=&mall_type=1&asc=0&page=1").get();
//			System.out.println(doc);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		String s = getHtmlData("http://www.feiniu.com/category/C28892?page=1");
		Document doc = Jsoup.parse(s);
		Elements elements = doc.getElementsByClass("listPic");
		if (elements.size() > 0) {
			for (int i = 0; i < elements.size(); i++) {
				Element element = elements.get(i).child(0);
				String url = element.attr("href");
				String src = element.child(0).attr("src");
				String interHtml = getHtmlData(url);
				Document interDoc = Jsoup.parse(interHtml);
				System.out.println("图片："+src);
//				
				Element e = interDoc.getElementById("detailBox");
				String name = e.children().first().html();
				System.out.println("名称："+name);//名称
				
				String price = e.getElementById("priceFn").getElementsByAttributeValue("itemprop", "highPrice").html();
				System.out.println("价格"+price);//价格
				
				//包装及规格
				String spec = interDoc.getElementsByClass("spec").get(0).html();
				System.out.println("规格："+spec);
				
				Elements es= interDoc.getElementsByClass("topic-path");
				Elements ae = es.get(0).getElementsByTag("a");
				System.out.print("分类:");
				for(int ia=0;ia<ae.size();ia++){
					System.out.print(ae.get(ia).html()+">");
					System.out.println();
				}
				System.out.println("---------");
					
//				String topic-path ="";
				
//				System.out.println(url);
//				getProductByUrl(urls);
			}
		}
//		System.out.println(s);
	}

}
